package com.tz.controller;

import com.tz.entity.User;
import com.tz.service.PerSonalCenterService;
import com.tz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/permission/emp")
//把放入到model中的数据放入到session中
@SessionAttributes("user")
public class PersonalCenterController {
    @Autowired
    PerSonalCenterService perSonalCenterService;
    @Autowired
    UserService userService;

//    @RequestMapping("/download")
//    public ResponseEntity<byte[]> download(String path, HttpServletRequest request) throws IOException {
//        String fullPath = request.getServletContext().getRealPath("/assets/img") + File.separator + path;
//        byte[] file = toByteArray(fullPath);
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentDispositionFormData("attachment", path);
//        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//        return new ResponseEntity<>(file, headers, HttpStatus.CREATED);
//    }

    @ResponseBody
    @RequestMapping(value = "/preedit", method = RequestMethod.POST)
    public User preedit(@SessionAttribute("user") User user) throws IOException {

        return user;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String edit(MultipartFile file, User user, HttpServletRequest request) throws IOException {


        if (!file.isEmpty()) {
            String path = request.getServletContext().getRealPath("/dist/img");
            perSonalCenterService.upload(path, file);
            user.setHeadurl(file.getOriginalFilename());
            file.transferTo(new File(path,file.getOriginalFilename()));


        }
        userService.changeUser(user);
        return "redirect:/user/index#/personal";
    }
//
//    private byte[] toByteArray(String filename) throws IOException {
//
//        File f = new File(filename);
//        if (!f.exists()) {
//            throw new FileNotFoundException(filename);
//        }
//
//        FileChannel channel = null;
//        FileInputStream fs = null;
//        try {
//            fs = new FileInputStream(f);
//            channel = fs.getChannel();
//            ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
//            while ((channel.read(byteBuffer)) > 0) {
//                // do nothing
//                // System.out.println("reading");
//            }
//            return byteBuffer.array();
//        } catch (IOException e) {
//            e.printStackTrace();
//            throw e;
//        } finally {
//            try {
//                assert channel != null;
//                channel.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            try {
//                fs.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
}

